package com.yic.module.mes.controller.admin.modetail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.mes.controller.admin.modetail.vo.*;
import com.yic.module.mes.dal.dataobject.modetail.MoDetailDO;
import com.yic.module.mes.convert.modetail.MoDetailConvert;
import com.yic.module.mes.service.modetail.MoDetailService;

@Tag(name = "管理后台 - 工单明细")
@RestController
@RequestMapping("/mes/mo-detail")
@Validated
public class MoDetailController {

    @Resource
    private MoDetailService moDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建工单明细")
    @PreAuthorize("@ss.hasPermission('mes:mo-detail:create')")
    public CommonResult<Long> createMoDetail(@Valid @RequestBody MoDetailCreateReqVO createReqVO) {
        return success(moDetailService.createMoDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单明细")
    @PreAuthorize("@ss.hasPermission('mes:mo-detail:update')")
    public CommonResult<Boolean> updateMoDetail(@Valid @RequestBody MoDetailUpdateReqVO updateReqVO) {
        moDetailService.updateMoDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:mo-detail:delete')")
    public CommonResult<Boolean> deleteMoDetail(@RequestParam("id") Long id) {
        moDetailService.deleteMoDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mo-detail:query')")
    public CommonResult<MoDetailRespVO> getMoDetail(@RequestParam("id") Long id) {
        MoDetailDO moDetail = moDetailService.getMoDetail(id);
        return success(MoDetailConvert.INSTANCE.convert(moDetail));
    }

    @GetMapping("/get/{serialNo}")
    @Operation(summary = "获得工单明细")
    @Parameter(name = "serialNo", description = "流水号", required = true, example = "1024")
    public CommonResult<MoDetailRespVO> getMoDetailBySerial(@PathVariable("serialNo") String serialNo) {
        MoDetailDO moDetail = moDetailService.getMoDetailBySn(serialNo);
        return success(MoDetailConvert.INSTANCE.convert(moDetail));
    }

    @GetMapping("/list")
    @Operation(summary = "获得工单明细列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('mes:mo-detail:query')")
    public CommonResult<List<MoDetailRespVO>> getMoDetailList(@RequestParam("ids") Collection<Long> ids) {
        List<MoDetailDO> list = moDetailService.getMoDetailList(ids);
        return success(MoDetailConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单明细分页")
    @PreAuthorize("@ss.hasPermission('mes:mo-detail:query')")
    public CommonResult<PageResult<MoDetailRespVO>> getMoDetailPage(@Valid MoDetailPageReqVO pageVO) {
        PageResult<MoDetailDO> pageResult = moDetailService.getMoDetailPage(pageVO);
        return success(MoDetailConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工单明细 Excel")
    @PreAuthorize("@ss.hasPermission('mes:mo-detail:export')")
    @OperateLog(type = EXPORT)
    public void exportMoDetailExcel(@Valid MoDetailExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MoDetailDO> list = moDetailService.getMoDetailList(exportReqVO);
        // 导出 Excel
        List<MoDetailExcelVO> datas = MoDetailConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "工单明细.xls", "数据", MoDetailExcelVO.class, datas);
    }

}
